#!/bin/bash

# $Header: /opt/unixmon/tools/DNS/RCS/named_diff_zones,v 1.1 2012/11/27 07:50:51 yajin Exp $

# Author: yang.jin@ebay.com
#
# this script compares the zones on /etc/named.zones and /etc/named.zones-new, or, on /etc/qa.zones 
# and /etc/qa.zones-new. and tells which zones to be added/removed after the "-new" file is moved 
# to production.

print_usage ()
{

echo -e "\nUsage:\n\t$(basename $0) [-mp|-qa]\n"


}

export PATH=/bin:/usr/bin

if [ $# -gt 1 ]; then print_usage; exit 1; fi


if [ $# -eq 0 ]; then
	opt="-mp"
else
	opt=$1
	if [ $opt != "-mp" ] && [ $opt != "-qa" ]; then
		print_usage; exit 1
	fi
fi

opt=$(echo $opt | sed -e 's/\-//')

OLD_ZONES=/etc/named/${opt}.zones
NEW_ZONES=/etc/named/${opt}.zones-new

if [ ! -f $OLD_ZONES ] || [ ! -f $NEW_ZONES ]; then
	echo "$OLD_ZONES or $NEW_ZONES doesn't exist." 
	exit 1
fi

/usr/xpg4/bin/grep "^[[:space:]]*zone[[:space:]]\{1,\}" $OLD_ZONES | awk '{print $2}' | sed -e 's/\"//g' -e 's/ *//g' | sort > /tmp/zone1.$$
/usr/xpg4/bin/grep "^[[:space:]]*zone[[:space:]]\{1,\}" $NEW_ZONES | awk '{print $2}' | sed -e 's/\"//g' -e 's/ *//g' | sort > /tmp/zone2.$$

diff /tmp/zone1.$$ /tmp/zone2.$$  > /tmp/zone3.$$

echo -e "\nThe following zones are to be added ...\n======================================="
egrep "^\>" /tmp/zone3.$$ | awk '{print $2}' | grep -v "^$"

echo -e "\n\nThe following zones are to be removed ...\n========================================="
egrep "^\<" /tmp/zone3.$$ | awk '{print $2}' | grep -v "^$"

echo 

rm -f /tmp/zone1.$$ /tmp/zone2.$$ /tmp/zone3.$$
